package com.jaymin.flink.datastream.watermark;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.Date;

/**
 * 输入数据的格式: 时间字段,单词，次数
 *
 * @author jaymin
 * @since 2024/4/6 23:50
 */
public class EventTimeWatermarkApp {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        test01(executionEnvironment);
        executionEnvironment.execute("EventTimeWatermarkApp");
    }

    private static void test01(StreamExecutionEnvironment executionEnvironment) {
        DataStreamSource<String> streamSource = executionEnvironment.readTextFile("data/wm.json");
        OutputTag<Tuple2<String, Integer>> outputTag = new OutputTag<Tuple2<String, Integer>>("late-data"){};
        // 允许5秒延迟到达，迟到会直接丢弃,可以指定outputTag输出
        SingleOutputStreamOperator<String> window = streamSource.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<String>(Duration.ofSeconds(5)) {
                    @Override
                    public long extractTimestamp(String element) {
                        WCDTO wcdto = JSONUtil.toBean(element, WCDTO.class);
                        Date date = wcdto.getDate();
                        long time = date.getTime();
                        return time;
                    }
                })
                .map(new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String value) throws Exception {
                        WCDTO wcdto = JSONUtil.toBean(value, WCDTO.class);
                        return Tuple2.of(wcdto.getWord(), wcdto.getCount());
                    }
                })
                .keyBy(x -> x.f0)
                .window(TumblingEventTimeWindows.of(Duration.ofDays(1)))
                .sideOutputLateData(outputTag)
                .reduce(new ReduceFunction<Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> reduce(Tuple2<String, Integer> value1, Tuple2<String, Integer> value2) throws Exception {
                        return Tuple2.of(value1.f0, value2.f1 + value1.f1);
                    }
                }, new ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {
                    @Override
                    public void process(String s, ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>.Context context, Iterable<Tuple2<String, Integer>> elements, Collector<String> out) throws Exception {
                        FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
                        String start = dateFormat.format(context.window().getStart());
                        String end = dateFormat.format(context.window().getEnd());
                        for (Tuple2<String, Integer> element : elements) {
                            out.collect(StrUtil.format("start:{},end:{},word:{},count:{}", start, end, element.f0, element.f1));
                        }
                    }
                });
        window.print();
        SideOutputDataStream<Tuple2<String, Integer>> sideOutput = window.getSideOutput(outputTag);
        sideOutput.printToErr();
    }


}
